/* interface/hiir/hiir.c
 *
 * Copyright (c) 2006 Hisilicon Co., Ltd. 
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
 *
 * History:
 *      16-12-2006 Start of Hi3560 CPU support
 */


#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/fs.h>
#include <asm/hardware.h>
#include <linux/interrupt.h>
#include <linux/poll.h>
#include <linux/miscdevice.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/console.h>
#include <linux/sysrq.h>
#include <linux/device.h>
#include <linux/clk.h>

#include <asm/io.h>
#include <asm/sizes.h>
#include <asm/arch/hardware.h>

#include "vo-base.h"



/* define IR IO */
#define VO_REG_BASE (IO_ADDRESS(0x20130000))

#define VO_BASE(reg)	(IO_ADDRESS(0x20130000 + reg))


/* define macro */
#define WRITE_REG(Addr, Value) ((*(volatile unsigned int *)(Addr)) = (Value))
#define READ_REG(Addr)         (*(volatile unsigned int *)(Addr))

unsigned long read_long(unsigned int reg)
{
	return readl(VO_BASE(reg));
}

unsigned int write_vo_base(vo_base_reg * vo)
{
	writel(vo->value, VO_BASE(vo->reg));
	return 0;
}

static int vo_all_reg(void)
{
	int i;

	for (i = 0; i < 0xff; i += 4) {
		readl(VO_REG_BASE + i);
	}

	return 0;
}

static int vo_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
    int __user *argp = (int __user *)arg;
	vo_base_reg vo;
	unsigned long reg;
	unsigned long value;

    switch(cmd)
    {
        case VO_BASE_GET_ALL_REG:
			vo_all_reg();
			break;

		case VO_BASE_GET_SIGNLE_REG:
			if (copy_from_user(&reg, argp, sizeof(reg))) {
				printk(KERN_ALERT "\t[ioctl error!] VO_BASE_GET_SIGNLE_REG\n");
				return -EINVAL;
			}
			value = read_long(reg);
			if (copy_to_user(argp, &value, sizeof(value)))
				return -1;

			break;

		case VO_BASE_SET_SIGNLE_REG:
			if (copy_from_user(&vo, argp, sizeof(vo))) {
				printk(KERN_ALERT "\t[ioctl error!] VO_BASE_SET_SIGNLE_REG\n");
				return -EINVAL;
			}
			write_vo_base(&vo);
			break;

        default:
            printk( "Error: Inappropriate ioctl for cmd\n");
            return -ENOTTY;
    }

    return 0;
}

static int vo_open(struct inode *inode, struct file *filp)
{
    return 0;
}

static int vo_close(struct inode *inode, struct file *filp)
{
    return 0;
}


static struct file_operations vo_fops =
{
    owner   : THIS_MODULE,
    open    : vo_open,
    ioctl   : vo_ioctl,
    release    : vo_close,
};

static struct miscdevice vo_miscdev =
{
    .minor = MISC_DYNAMIC_MINOR,
    .name  = VO_DEVICE_NAME,
    .fops  = &vo_fops,
};

static int __init vo_init(void)
{
    int res = 0;

    //printk("\tvo modify!\n");

	res = misc_register(&vo_miscdev);
	if (0 != res)
	{
		printk(KERN_DEBUG "Error: can't register\n");
		return -EFAULT;
	}
        
    printk("\tvo base!.\n");

    return res;
}

static void __exit vo_exit(void)
{
    misc_deregister(&vo_miscdev);
}

module_init(vo_init);
module_exit(vo_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("VI Base Device Driver");
MODULE_VERSION("HI_VERSION=" OSDRV_MODULE_VERSION_STRING);



